﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xierun.IServices;
using Xierun.Services;
using Xierun.WebApp.Extensions;
using Xierun.WebApp.Loggers;

namespace Xierun.WebApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        public static readonly LoggerFactory EFLoggerFactory = new LoggerFactory(new[] { new EFLoggerProvider() });

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {

            services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddDbContext<EntityContext>(options =>
            {
                options.UseSqlServer(
                    Configuration.GetConnectionString("MssqlConnection")).UseLoggerFactory(EFLoggerFactory);
                //options.UseInMemoryDatabase(Configuration.GetConnectionString("MssqlConnection"));
            });

            services.AddScoped<IBase_UserService, Base_UserService>();

            services.AddCors(option =>
                option.AddPolicy("cors", policy =>
                    //policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(new[] { "http://127.0.0.1:8848" }))
                    policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().AllowAnyOrigin())
                );

            services.AddAutoMapper(Assembly.GetExecutingAssembly());
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            return new AutofacServiceProvider(AutofacExt.InitAutofac(services));
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseExceptionJsonHandler();


            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseCors("cors");
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "area",
                    template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
